<?php


namespace app\store\controller;


use app\common\model\Store;
use app\common\model\Users;
use app\common\util\AdminException;
use think\App;
use think\Facade\Session;

class Admin extends Base
{
    public function login(){
        if(IS_POST){
            $mobile=input('mobile');
            $password=input('password');
            $captcha=input('captcha');

            if (empty($mobile)){
                throw new AdminException('请填写登录账号');
            }
            if (empty($password)){
                throw new AdminException('请填写密码');
            }
            $user=Users::where('mobile',$mobile)->find();
            if (empty($user)){
                throw new AdminException('账号不存在');
            }
            $store=$user['store'];
            if (empty($store)){
                throw new AdminException('商家不存在');
            }
            if (encrypt($password)!=$user['password']){
                throw new AdminException('账号或密码错误');
            }
            if (empty($captcha)){
                throw new AdminException('请填写验证码');
            }
            if (!captcha_check($captcha)){
                throw new AdminException('验证码不正确',10);
            }
            if ($store['status']!=1){
                throw new AdminException('未开通店铺或店铺异常');
            }
            $res = session('dianpu_id',$store['store_id']);
            throw new AdminException('登录成功',1,['url'=>url('Index/index')->build()]);
        }

        if (session('?dianpu_id') && session('dianpu_id') > 0) {
            $this->error("您已登录", url('User/index')->build());
        }
        return view();
    }
    //退出登录
    public function logout(){
        session('dianpu_id',null);
        $url=url('admin/login')->build();
        return $this->redirect($url);
    }
}
